/*
 * @lc app=leetcode.cn id=21 lang=java
 *
 * [21] 合并两个有序链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode();
        ListNode cur = dummy;

        ListNode l3 = l1;
        ListNode l4 = l2;

        while (l3 != null && l4 != null) {
            if (l3.val < l4.val) {
                cur.next = l3;
                l3 = l3.next;
                cur = cur.next;
            } else {
                cur.next = l4;
                l4 = l4.next;
                cur = cur.next;
            }
        }

        while (l3 != null) {
            cur.next = l3;
            l3 = l3.next;
            cur = cur.next;
        }

        while (l4 != null) {
            cur.next = l4;
            l4 = l4.next;
            cur = cur.next;
        }

        return dummy.next;
    }
}
// @lc code=end

